package com.example.myapplication3.image.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.myapplication3.R;
import com.example.myapplication3.image.bean.ImageItem;

import java.io.File;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ImageItem> datas;
    private Context context;

    public ImageAdapter(List<ImageItem> imageList, Context context) {
        datas = imageList;
        this.context = context;
    }

    private int mode = MODE_LIST;
    public static final int MODE_LIST = 0;
    public static final int MODE_GRID = 1;
    public void setMode(int mode) {
        this.mode = mode;
    }
    @Override
    public int getItemViewType(int position) {
        return mode;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View itemView;
        switch (viewType){
            case MODE_GRID:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_grid_, parent,false);
                return new GridViewHolder(itemView);
            case MODE_LIST:
            default:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_list_, parent, false);
                return new ListViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ImageItem imageItem = datas.get(position);
        // 列表模式
        if (holder instanceof ListViewHolder) {
            ListViewHolder viewHolder = (ListViewHolder) holder;
            viewHolder.tv_title.setText(imageItem.getName());
            viewHolder.tv_size.setText(imageItem.getFormattedSize());
            viewHolder.tv_create_time.setText(imageItem.getFormattedDate());
            Glide.with(context)
                    .load(new File(imageItem.getPath()))
                    .placeholder(R.drawable.laoding)
                    .error(R.drawable.ic_image)
                    .override(160, 160)
                    .transform(new RoundedCorners(16)) // 圆角半径（单位：px）
                    .into(viewHolder.iv_icon);
            return;
        }
        // 网格模式
        if (holder instanceof GridViewHolder) {
            GridViewHolder viewHolder = (GridViewHolder) holder;
            Glide.with(context)
                    .load(new File(imageItem.getPath()))
                    .placeholder(R.drawable.laoding)
                    .error(R.drawable.ic_image)
                    .override(160, 160)
                    .centerCrop()
                    .into(viewHolder.iv_icon);
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ListViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_icon;
        TextView tv_title;
        TextView tv_size, tv_create_time;

        public ListViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_size = itemView.findViewById(R.id.tv_size);
            tv_create_time = itemView.findViewById(R.id.tv_create_time);

            itemView.setOnClickListener(v -> {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(v, getAdapterPosition());
                }
            });
            itemView.setOnLongClickListener(v -> {
                if (itemOnLongClickListener != null) {
                    itemOnLongClickListener.onLongClick(v, getAdapterPosition());
                }
                return false;
            });
        }
    }
    class GridViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_icon;
        public GridViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.imageview);
            //tv_title = itemView.findViewById(R.id.tv_title);

            // item项监听
            itemView.setOnClickListener(v -> {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(v, getAdapterPosition());
                }
            });
            itemView.setOnLongClickListener(v -> {
                itemView.setClickable(true);
                if (itemOnLongClickListener != null) {
                    itemOnLongClickListener.onLongClick(v, getAdapterPosition());
                }
                return false;
            });
        }
    }
    private ItemOnClickListener itemOnClickListener;
    private ItemOnLongClickListener itemOnLongClickListener;
    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener){
        this.itemOnClickListener=itemOnClickListener;
    }
    public void setItemOnLongClickListener(ItemOnLongClickListener itemOnLongClickListener){
        this.itemOnLongClickListener=itemOnLongClickListener;
    }
    public interface ItemOnClickListener { void onClick(View v, int position);   }

    public interface ItemOnLongClickListener { void onLongClick(View v, int position);  }
}